package com.kamistoat.meimeistore.coupon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.common.To.SecKillTo.SecKillSessionTo;
import com.kamistoat.common.constant.SecKillConstant;
import com.kamistoat.meimeistore.coupon.feign.SecKillFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeistore.coupon.entity.SeckillSessionEntity;
import com.kamistoat.meimeistore.coupon.service.SeckillSessionService;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;



/**
 * 秒杀活动场次
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 16:19:46
 */
@RestController
@RequestMapping("meimeistorecoupon/seckillsession")
public class SeckillSessionController {
    @Autowired
    private SeckillSessionService seckillSessionService;
    @Autowired
    private SecKillFeignService secKillFeignService;

    /**
     * 查询出最近三天的秒杀活动以及对应的所有商品
     * @return
     */
    @GetMapping("/get3DaySession")
    public List<SecKillSessionTo> get3DaySession(){
        List<SecKillSessionTo> sessionList = seckillSessionService.get3DaySession();
        return sessionList;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistorecoupon:seckillsession:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = seckillSessionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meimeistorecoupon:seckillsession:info")
    public R info(@PathVariable("id") Long id){
		SeckillSessionEntity seckillSession = seckillSessionService.getById(id);

        return R.ok().put("seckillSession", seckillSession);
    }

    /**
     * 当管理系统添加秒杀场次时，除了进MySQL，还检查该秒杀是否位于3天以内，如果是，同时在redis进行缓存。
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistorecoupon:seckillsession:save")
    public R save(@RequestBody SeckillSessionEntity seckillSession){
        R status = seckillSessionService.checkSeckillSession(seckillSession);
        if((int)status.get("code") == 0){
            seckillSessionService.save(seckillSession);
            secKillFeignService.delete_ReUp_RedisSessions(getRedisSessionKey(new Long[]{seckillSession.getId()}));
            return status;
        }else {
            return status;
        }
    }

    /**
     * 当管理系统修改秒杀场次时，除了进MySQL，还检查该秒杀是否位于3天以内，如果是，同时在redis删除旧缓存，缓存新秒杀。
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistorecoupon:seckillsession:update")
    public R update(@RequestBody SeckillSessionEntity seckillSession){
        R status = seckillSessionService.checkSeckillSession(seckillSession);
        if((int)status.get("code") == 0){
            seckillSessionService.updateById(seckillSession);
            secKillFeignService.delete_ReUp_RedisSessions(getRedisSessionKey(new Long[]{seckillSession.getId()}));
            return status;
        }else{
            return status;
        }
    }

    /**
     * 删除某一场秒杀活动，删除的同时还要检查redis中是否存在已上架的秒杀活动。如果存在则需要将其一并删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistorecoupon:seckillsession:delete")
    public R delete(@RequestBody Long[] ids){
        List<String> redisSessionKey = getRedisSessionKey(ids);
		seckillSessionService.removeByIds(Arrays.asList(ids));
        secKillFeignService.delete_ReUp_RedisSessions(redisSessionKey);
        return R.ok();
    }

    /**
     * 根据ids制作redis keyList
     * @param ids
     * @return
     */
    public List<String> getRedisSessionKey(@RequestParam Long[] ids){
        return seckillSessionService.listByIds(Arrays.asList(ids)).stream().map(seckillSessionEntity -> {
            String key = SecKillConstant.SEC_SESSION_PREFIX
                    + seckillSessionEntity.getStartTime().getTime() + "_" + seckillSessionEntity.getEndTime().getTime();
            return key;
        }).collect(Collectors.toList());
    }

}
